/*
 *	dfclient_get.c
 *	© 2009 DigiFi Limited
 *
 *	Contains the functions that get single values
 *
 */

#include "../headers/dfget.h"room_id



int dfget_volume(unsigned int room_id, void (*callback)(int)){
	return df_queuecommand(DF_GET_COMMAND, "getvolume", (void (*)(void))callback, "[GetVolume %d]", room_id);
}


int dfget_playerstatus(unsigned int room_id, void (*callback)(char*)){
	return df_queuecommand(DF_GET_COMMAND, "getplayerstatus", (void (*)(void))callback, "[GetPlayerStatus %d]", room_id);
}


int dfget_repeat(unsigned int room_id, void (*callback)(int)){
	return df_queuecommand(DF_GET_COMMAND, "getrepeat", (void (*)(void))callback, "[GetRepeat %d]", room_id);
}


int dfget_shuffle(unsigned int room_id, void (*callback)(int)){
	return df_queuecommand(DF_GET_COMMAND, "getshuffle", (void (*)(void))callback, "[GetShuffle %d]", room_id);
}


int dfget_trackname(unsigned int room_id, void (*callback)(df_track*)){
	return df_queuecommand(DF_GET_COMMAND, "gettrackname", (void (*)(void))callback, "[GetTrackName %d]", room_id);
}


int dfget_tracknum(unsigned int room_id, void (*callback)(int)){
	return df_queuecommand(DF_GET_COMMAND, "gettracknum", (void (*)(void))callback, "[GetTrackNum %d]", room_id);
}


int dfget_trackpos(unsigned int room_id, void (*callback)(df_time*)){
	return df_queuecommand(DF_GET_COMMAND, "gettrackposition", (void (*)(void))callback, "[GetTrackPosition %d]", room_id);
}


int dfget_albumartist(unsigned int room_id, void (*callback)(df_albumartist*)){
	return df_queuecommand(DF_GET_COMMAND, "getalbumartist", (void (*)(void))callback, "[GetAlbumArtist %d]", room_id);
}

int dfget_checkcoverimage(char *address, int image_size, int address_type, int local, char *protocol, char *image_format, void (*callback)(char*, char*)) {
	return df_queuecommand(DF_GET_COMMAND, "checkcoverimage", (void (*)(void))callback,
	 			"[CheckCoverImage \"%s\" %d %d %d \"%s\" \"%s\"]", address, image_size, address_type, local, protocol, image_format);
}

int dfget_librarychecksum(unsigned int room_id, void (*callback)(int)) {
	return df_queuecommand(DF_GET_COMMAND, "getlibrarychecksum", (void (*)(void))callback, "[GetLibraryChecksum %d]", room_id);
}




int dfget_callbackhandler(char *regex_name, dfrx_result *rx, void (*callback)(void)) {
	df_time *t;
	df_albumartist *aa;
	df_track *tr;
	int i;
	char *s, *s1;
	
	if (strcmp(regex_name, "getvolume") == 0) {
		sscanf(rx->subexps[2].value,"%d",&i);
		
		((void (*)(int))callback)(i);
	}
	else if (strcmp(regex_name, "getplayerstatus") == 0) {
		if ((s = strdup(rx->subexps[2].value)) == NULL) {return -DFCLIENT_ENOMEM;}
		
		((void (*)(char*))callback)(s);
		
		free(s);
	}
	else if (strcmp(regex_name, "checkcoverimage") == 0) {
		if ((s = strdup(rx->subexps[2].value)) == NULL) {return -DFCLIENT_ENOMEM;}
		if ((s1 = strdup(rx->subexps[3].value)) == NULL) {return -DFCLIENT_ENOMEM;}
		
		((void (*)(char*,char*))callback)(s, s1);
		
		free(s);
		free(s1);
	}
	else if (strcmp(regex_name, "getlibrarychecksum") == 0) {
		sscanf(rx->subexps[2].value,"%d",&i);
		
		((void (*)(int))callback)(i);
	}
	else if (strcmp(regex_name, "getrepeat") == 0) {
		sscanf(rx->subexps[2].value,"%d",&i);
		
		((void (*)(int))callback)(i);
	}
	else if (strcmp(regex_name, "getshuffle") == 0) {
		sscanf(rx->subexps[2].value,"%d",&i);
		
		((void (*)(int))callback)(i);
	}
	else if (strcmp(regex_name, "gettrackname") == 0) {
		if ((tr = malloc(sizeof(df_track))) == NULL) {return -DFCLIENT_ENOMEM;}

		sscanf(rx->subexps[2].value,"%d",&(tr->key));
		if ((tr->name = strdup(rx->subexps[3].value)) == NULL) {return -DFCLIENT_ENOMEM;}
		if ((tr->type = strdup(rx->subexps[4].value)) == NULL) {return -DFCLIENT_ENOMEM;}
		if ((tr->length = malloc(sizeof(df_time))) == NULL) {return -DFCLIENT_ENOMEM;}
		sscanf(rx->subexps[5].value,"%u:%u:%u",
					&(tr->length->hours), &(tr->length->minutes), &(tr->length->seconds));
		sscanf(rx->subexps[6].value,"%d",&(tr->source));
		
		((void (*)(df_track*))callback)(tr);
		
		free(tr->name);
		free(tr->type);
		free(tr->length);
		free(tr);
	}
	else if (strcmp(regex_name, "gettracknum") == 0) {
		sscanf(rx->subexps[2].value,"%d",&i);
		
		((void (*)(int))callback)(i);
	}
	else if (strcmp(regex_name, "gettrackposition") == 0) {
		if ((t = malloc(sizeof(df_time))) == NULL) {return -DFCLIENT_ENOMEM;}
		sscanf(rx->subexps[2].value,"%u:%u:%u",&(t->hours), &(t->minutes), &(t->seconds));
		
		((void (*)(df_time*))callback)(t);
		
		free(t);
	}
	else if (strcmp(regex_name, "getalbumartist") == 0) {
		if ((aa = malloc(sizeof(df_albumartist))) == NULL) {return -DFCLIENT_ENOMEM;}
		if ((aa->album = strdup(rx->subexps[2].value)) == NULL) {return -DFCLIENT_ENOMEM;}
		if ((aa->artist = strdup(rx->subexps[3].value)) == NULL) {return -DFCLIENT_ENOMEM;}
		
		((void (*)(df_albumartist*))callback)(aa);
		
		free(aa->album);
		free(aa->artist);
		free(aa);
	}
	
	return 0;
}
